package com.longrise.android.byjk.plugins.tabthird.invoice;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.android.byjk.widget.view.BBswipeRefreshLayout;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerFather;
import com.longrise.common.base.BaseActivity2;
import com.longrise.common.datasource.remote.LoadDataManager;
import com.longrise.common.utils.AppUtil;
import com.longrise.common.utils.NetUtil;

import static com.longrise.android.byjk.R.id.ll_consume_null;

/**
 * Created by Administrator on 2017/10/20.
 *开票历史
 */

public class BillHistoryActivity extends BaseActivity2 implements SwipeRefreshLayout.OnRefreshListener,
        BaseQuickAdapter.OnItemClickListener {

    private BBswipeRefreshLayout mSwipelayout;
    private RecyclerView mRcv;
    private BillHistoryAdapter mRcvAdapter;
    private EntityBean[] mBillHistorybeans;

    public static final String BILL_HISTORY = "bill_history";

    public static final int BILL_HISTORY_ACTIVITY = 2;
    private LinearLayout ll_bill_null;


    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_bill_history;
    }

    @Override
    public void initView() {
        mSwipelayout = (BBswipeRefreshLayout)findViewById(R.id.swipe_layout_bill_history);
        mRcv = (RecyclerView)findViewById(R.id.recyview_bill_history);
        ll_bill_null = (LinearLayout) findViewById(R.id.ll_bill_null);
        initRcv();
        initData();
    }

    @Override
    public void onToolbarBackClick() {
        finish();
    }

    private void initRcv(){
        setToolbarTitle(AppUtil.getString(R.string.bill_history));
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRcv.setLayoutManager(linearLayoutManager);
        mRcvAdapter = new BillHistoryAdapter();
        mRcv.setAdapter(mRcvAdapter);
        mRcvAdapter.setOnItemClickListener(this);
        mSwipelayout.setOnRefreshListener(this);
    }


    private void openCourseDetail(EntityBean bean) {
        Intent intent =new Intent(this,BillDetailActivity.class);
        intent.putExtra(BillDetailActivity.INVOICE_STATE,bean.getString("invstate", ""));//处理中,已邮寄
        intent.putExtra(BillDetailActivity.INVOICE_ID,bean.getString("id", ""));//发票id
        intent.setFlags(BILL_HISTORY_ACTIVITY);
        startActivity(intent);
    }

    @Override
    public void onRefresh() {
        clientRequest();
    }

    private void initData() {
        if (NetUtil.checkNetEnable()) {
            mSwipelayout.setRefreshing(true);
            clientRequest();
        }
    }

    private void clientRequest() {
        EntityBean bean = new EntityBean();
        bean.set("cardno", UserInfor.getInstance().getUsersfzh());
        EntityBean bean1 = new EntityBean();
        bean1.set("bean", bean);
        LoadDataManager.getInstance().callService2(null, UrlConstants.BaseUrl, "searchPreinvoiceByCardno", bean1, new LoadDataManager.OnRequestCallListener2() {

            @Override
            public void onSuccess(String key, String service, Object result) {
                try {
                    parse(result);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }


            @Override
            public void onFinished(String key, String service) {
                mSwipelayout.setRefreshing(false);
            }

            @Override
            public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {

            }

        });
    }

    private void parse(Object result) {
        EntityBean bean = (EntityBean) result;
        mBillHistorybeans = (EntityBean[]) bean.get("result");
        if(mBillHistorybeans != null){
            mRcvAdapter.refreshData(mBillHistorybeans);
            ll_bill_null.setVisibility(View.GONE);
            mSwipelayout.setVisibility(View.VISIBLE);
        }else {
            ll_bill_null.setVisibility(View.VISIBLE);
            mSwipelayout.setVisibility(View.GONE);
        }

    }


    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        final EntityBean bean = mBillHistorybeans[position];
        openCourseDetail(bean);
    }
}
